


#include "EIniTabFile.h"

using namespace std;

EIniTabFile::EIniTabFile()
{
	Init();
}

EIniTabFile::~EIniTabFile()
{
	Init();
}

void EIniTabFile::Release()
{
	delete this;
}

int EIniTabFile::LoadTitleRelation( FILE* pfile )
{
	_tablefile_string _TitleContent;
	char ch;
	int nWide =  0;
	do
	{
		fscanf_s( pfile,"%s", _TitleContent.szBuf );
		ch = fgetc(pfile);
		m_TitleRelation[_TitleContent] = nWide;
		nWide++;
	}
	while(strcmp(_TitleContent.szBuf,"") != 0 && ch != '\n');

	if ( 1 == nWide )
		nWide = 0;

	return nWide;
}

int EIniTabFile::GetContentsHigh()
{
	return GetHigh() - TABLE_FILE_CONTENTS_BEGIN_ROW;
}
int	 EIniTabFile::LoadContents( FILE* pfile )
{
	int nRow = TABLE_FILE_CONTENTS_BEGIN_ROW;
	_tablefile_string _Content;
	char ch = 0;
	bool _bEndLineFlag = false;
	STabFilePos _pos;

	while( !feof( pfile ) )
	{
		for ( int nCol = 0; nCol < m_nWide; nCol++ )
		{
			ch = fgetc( pfile );
			if ( '\t' == ch || '\n' == ch )
			{
				memset( _Content.szBuf, 0, sizeof( _Content.szBuf ) );
				_bEndLineFlag = true;
			}
			else if ( -1 == ch )
			{
				return --nRow + TABLE_FILE_CONTENTS_BEGIN_ROW;
			}
			else
				_bEndLineFlag = false;

			if (!_bEndLineFlag)
			{
				fseek( pfile, -1L, SEEK_CUR );
				fscanf_s( pfile,"%s", _Content.szBuf );
				ch = fgetc( pfile );
			}

			_pos.nCol = nCol;
			_pos.nRow = nRow;

			m_FileContents[_pos] = _Content;


		}
		_bEndLineFlag = false;
		nRow++;
	}


	return nRow;
}


bool EIniTabFile::GetFloat( IN int nRow, IN LPSTR szColName, IN float fDefaultVal, OUT float* fpOut )
{
	char szBuf[S_INI_TABLE_CONTENTS_LONG];
	if ( !GetContents( nRow, szColName, szBuf ) )
	{
		*fpOut = fDefaultVal;
		return false;
	}

	*fpOut = (float)(atof(szBuf));
	return true;
}
bool EIniTabFile::GetInteger( IN int nRow, IN LPSTR szColName, IN int nDefaultVal, OUT int* npOut )
{
	char szBuf[S_INI_TABLE_CONTENTS_LONG];
	if ( !GetContents( nRow, szColName, szBuf ) )
	{
		*npOut = nDefaultVal;
		return false;
	}

	*npOut = atoi(szBuf);
	return true;
}
bool	EIniTabFile::GetString( IN int nRow, IN LPSTR szColName, IN LPSTR szDefaultVal , OUT LPSTR szpOut )
{
	char szBuf[S_INI_TABLE_CONTENTS_LONG];
	if ( !GetContents( nRow, szColName, szBuf ) )
	{
		strcpy_s(szpOut, sizeof(szpOut), szDefaultVal);
		return false;
	}
	strcpy_s(szpOut, sizeof(szpOut), szBuf);
	return true;
}


bool	EIniTabFile::GetUnsignedLong( IN int nRow, IN LPSTR szColName, IN ULONG ulDefaultVal, OUT ULONG* ulpOut  )
{
	char szBuf[S_INI_TABLE_CONTENTS_LONG];
	if ( !GetContents( nRow, szColName, szBuf ) )
	{
		*ulpOut = ulDefaultVal;
		return false;
	}

	*ulpOut = strtoul( szBuf, NULL, 0 );
	return true;
}




EIIniTabFile*	CreateIniTabFile()
{
	return (new EIniTabFile);
}